package com.shenfa.util.sequence;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 获取序列工具类
 * 
 * @author David
 *
 */
public class SequenceUtil {

	@Autowired
	private SequenceDao seqDao;
	
	private static final Logger log = Logger.getLogger(SequenceUtil.class);
	
	/** NetServer参数表 T_S_NetServer_PARA */
	public final static int SEQ_NETSERVER_PARA = 1;
	
	/** 账单参数  T_S_BILL_PARA */
	public final static int SEQ_BILL_PARA = 2;
	              
	/** 插页与产品关系表 T_S_FOLDOUT_PROD */
	public final static int SEQ_FOLDOUT_PROD = 3;
	
	/** T_S_PERSON[人员表]  */
	public final static int SEQ_PERSON = 4;
	
	/** T_S_ROLE [角色表] */
	public final static int SEQ_ROLE = 5;
	
	/** T_S_STENCIL[模版主表] */
	public final static int SEQ_STENCIL = 6;
	
	/** T_S_FODDER[素材表] */
	public final static int SEQ_FODDER = 7;
	
	/** T_S_RULE_M[规则表主表] */
	public final static int SEQ_RULE_M = 8;
	
	/** T_S_EPLBOLY_PNT[外包商表] */
	public final static int SEQ_EPLBOLY_PNT = 9;
	
	/** T_S_DEPARTMENT[部门表] */
	public final static int SEQ_DEPARTMENT = 10;
	
	/** t_s_organization[机构表] */
	public final static int SEQ_ORGANIZATION = 11;
	
	/** 其他表，类型为Integer */
	public final static int SEQ_OTHER_INTEGER = 12;
	
	/** T_S_FUNCTION[功能表] */
	public final static int SEQ_FUNCTION = 13;
	
	/** T_S_BEFOR_PRINT_BASIC[预印纸信息基础表] */
	public final static int SEQ_BEFOR_PRINT_BASIC = 14;

	/** T_S_BEFOR_PRINT_BASIC[预印纸信息基础表] */
	public final static int SEQ_STENCIL_IMAGE = 15;
		
	
	/**
	 * 获取序号
	 * @param sequenceType 序号类型，判断返回位数   .g. SequenceUtil.SEQ_NETSERVER_PARA
	 * @return 序号
	 * @throws Exception
	 */
	synchronized public String getSequence(int sequenceType){
		
		try {
			String seq = null;
			String preFix = "";
			String seqName = null;

			int seqLength = -1;

			switch (sequenceType) {
				case SEQ_NETSERVER_PARA:
					seqLength = 7;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_BILL_PARA:
					seqLength = 7;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_FOLDOUT_PROD:
					seqLength = 8;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_PERSON:
					preFix = "P";
					seqLength = 10;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_ROLE:
					preFix = "R";
					seqLength = 7;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_STENCIL:
					preFix = "S";
					seqLength = 7;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_FODDER:
					preFix = "F";
					seqLength = 7;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_RULE_M:
					preFix = "RM";
					seqLength = 6;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_EPLBOLY_PNT:
					seqLength = 4;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_DEPARTMENT:
					preFix = "DEP";
					seqLength = 5;
					seqName = "SEQ_NETSERVER_PARA";
					break;	
				case SEQ_ORGANIZATION:
					preFix = "ORG";
					seqLength = 5;
					seqName = "SEQ_NETSERVER_PARA";
					break;		
				case SEQ_OTHER_INTEGER:
					seqLength = 8;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_FUNCTION:
					seqLength = 8;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_BEFOR_PRINT_BASIC:
					preFix = "YYZ";
					seqLength = 5;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				case SEQ_STENCIL_IMAGE:
					preFix = "IMG";
					seqLength = 5;
					seqName = "SEQ_NETSERVER_PARA";
					break;
				default:
					throw new Exception("Illegal parameter.");
			}
			seq = formatSeq(seqDao.getSeq(seqName), seqLength);
			return preFix + seq;
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}

	/**
	 * 将sequence格式化
	 * 
	 * @param seq sequence
	 * @param len 格式化位数
	 * @return 格式化之后的字符串
	 * @throws Exception 传入的sequence为null，或者长度>len
	 */
	private static String formatSeq(String seq, int len) throws Exception {
		if (null == seq || seq.length() > len) {
			throw new Exception("Illegal parameter.");
		}
		StringBuffer result = new StringBuffer();
		while (result.length() + seq.length() != len) {
			result.append("0");
		}
		result.append(seq);
		return result.toString();
	}
}
